package com.zou.controller.backstage;

import com.zou.commons.constants.Constants;
import com.zou.commons.domain.ReturnObject;
import com.zou.pojo.Manager;
import com.zou.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class ManagerController {
    @Autowired
    private ManagerService managerService;

    /**
     * 展示满足条件的管理员列表
     *
     * @param manager
     * @param model
     * @return
     */
    @RequestMapping("/backstage/manager/list")
    public String list(Manager manager, Model model) {
        //调用service层方法，查询用户
        List<Manager> managers = managerService.querySomeManagerByManager(manager);
        model.addAttribute("managers", managers);
        return "backstage/manager/list.html";
    }

    /**
     * 进入管理员添加页面
     *
     * @return
     */
    @GetMapping("/backstage/manager/add")
    public String add() {
        return "backstage/manager/add.html";
    }

    /**
     * 添加管理员
     *
     * @param manager
     * @return
     */
    @PostMapping("/backstage/manager/add")
    public @ResponseBody
    Object add(Manager manager) {
        ReturnObject returnObject = new ReturnObject();
        if (managerService.addManager(manager)) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
        } else {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙，请稍后重试");
        }
        return returnObject;
    }

    /**
     * 批量删除管理员
     *
     * @param arr
     * @return
     */
    @PostMapping("/backstage/manager/del")
    public @ResponseBody
    Object del(String[] arr) {
        ReturnObject returnObject = new ReturnObject();
        if (managerService.deleteManagerByIds(arr)) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
        } else {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙，请稍后重试");
        }
        return returnObject;
    }

    /**
     * 根据id查询对应管理员，进入修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/backstage/manager/edit/{id}")
    public String edit(@PathVariable String id, Model model) {
        Manager manager = managerService.queryManagerById(id);
        model.addAttribute("manager", manager);
        return "backstage/manager/edit.html";
    }

    @PostMapping("/backstage/manager/edit")
    public @ResponseBody
    Object edit(Manager manager) {
        ReturnObject returnObject = new ReturnObject();
        if (managerService.updateManager(manager)) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
        } else {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙，请稍后重试");
        }
        return returnObject;
    }
}
